<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="riskPoint">
        <el-input v-model="queryParams.riskPoint" placeholder="请输入风险点" clearable size="middle" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="" prop="riskLevel">
        <el-select v-model="queryParams.riskLevel" placeholder="请选择风险等级" clearable size="middle" @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <!-- <el-table-column label="序号" align="center" index="index" /> -->
      <el-table-column label="风险点" align="center" prop="riskPoint" />
      <el-table-column label="危险源" align="center" prop="hazardSource" />
      <el-table-column label="危险因素" align="center" prop="riskFactor" />
      <el-table-column label="风险等级" align="center" prop="riskLevel" />
      <el-table-column label="危险源分类" align="center" prop="hazardClassId">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.HAZARD_CLASS" :value="scope.row.hazardClassId" />
        </template>
      </el-table-column>
      <el-table-column label="所属单位" align="center" prop="companyName" />
      <el-table-column label="直接责任人" align="center" prop="directPerson" />
      <el-table-column label="管控措施" align="center" prop="controlMeasures" />
      <el-table-column label="管控层级" align="center" prop="controlLevelId">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.CONTROL_LEVEL" :value="scope.row.controlLevelId" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['biz:risk-info:update']">设置</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="管控层级" prop="controlLevelId">
          <el-select v-model="form.controlLevelId" placeholder="请选择管控层级">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.CONTROL_LEVEL)"
                      :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="职责要求" prop="responseRequier">
          <el-input v-model="form.responseRequier" placeholder="请输入职责要求" />
        </el-form-item>
     </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { updateRiskInfo, getRiskInfo, getRiskInfoPage } from "@/api/biz/riskInfo";
import Editor from '@/components/Editor';
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";


export default {
  name: "RiskInfo",
  components: {
    Editor,
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 风险数据列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRangeIdentificatDate: [],
      dateRangeControlDate: [],
      dateRangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        riskPoint: null,
        riskLevel: null,
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
      },
      activeName: 'first',
      riskMethodNameList:[],
      //检查部门
      checkDeptOptions:undefined,
      // 用户下拉列表
      users: [],
      //风险等级下拉框
      options: [{
          value: '低风险',
          label: '低风险'
        }, {
          value: '一般风险',
          label: '一般风险'
        }, {
          value: '重大风险',
          label: '重大风险'
        }]
    };
  },
  created() {
    this.getList();
  },
  mounted(){
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangeIdentificatDate, 'identificatDate');
      this.addBeginAndEndTime(params, this.dateRangeControlDate, 'controlDate');
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行查询
      getRiskInfoPage(params).then(response => {
        this.list = response.list;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        controlLevelId: undefined,
        responseRequier: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeIdentificatDate = [];
      this.dateRangeControlDate = [];
      this.dateRangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getRiskInfo(id).then(response => {
        console.log(response)
        this.form = response.data;
        this.open = true;
        this.title = "设置管控层级";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateRiskInfo(this.form).then(response => {
            this.$modal.msgSuccess("设置成功");
            this.open = false;
            this.getList();
          });
          return;
        }
      });
    }
  }
};
</script>
